package start;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import start.log.Address;

import java.util.*;

public class EntityParse {


    public static void main(String[] args) {

        // Create a sample object
        SampleObject sampleObject = new SampleObject();
        sampleObject.setId(1);
        sampleObject.setName("John");


        Address address = new Address();
        address.setCity("New York");
        address.setProvince("USA");


        sampleObject.getAddress().add(address);

        // Convert object to JSON string
        String jsonString = JSON.toJSONString(sampleObject);

        // Parse JSON string to a Map
        Map<String, Object> map = JSON.parseObject(jsonString, HashMap.class);

        if(map.get("address") instanceof List) {
            JSONArray jsonArray = (JSONArray)map.get("address");
            List<Address> javaObject = JSONArray.parseArray(jsonArray.toJSONString(), Address.class);
            for (Address address1 : javaObject) {
                System.out.println(address1.getCity());
            }


            /*List<Address> addressList =  JSON.parseObject(, Address.class) ;
            for (Address address1 : addressList) {
                System.out.println(address1.getCountry());
            }*/

        }

        // Print the resulting map
        System.out.println(map);
    }

    // SampleObject class with nested Address class

}


class SampleObject {
    private int id;
    private String name;
    private List<Address> address = new ArrayList<>();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Address> getAddress() {
        return address;
    }

    public void setAddress(List<Address> address) {
        this.address = address;
    }
}


